<?php

namespace App\Http\Controllers\Admin;

use App\Category;
use App\Http\Response;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CategoryController extends Controller
{
    use Response;


    public function index(Request $request)
    {
        $type=$request->get('type');
        if ($type=='all'){
            $data=Category::all();
            return $this->success(200,$data);
        }else{
//            $paginate=Category::where([['mch_id','=',$this->mch_id]])->where('lang',$this->lang)->paginate($request->get('pageSize'),'*','pageNo');
            $categories=Category::where([['status','=',1]])->orderBy('sort','desc')->paginate($request->get('pageSize'),'*','pageNo');
            return $this->success(200,$categories);
        }
    }
    public function list(Request $request)
    {
        $categories=Category::where([['status','=',1]])->lang($this->lang)->orderBy('sort','desc')->get()->toArray();
        $data=$this->generateTree2($categories,0,$request->get('id'),0);
        return $this->success(200,$data);
    }
    public function show($id)
    {
        $result=Category::find($id);
        return $this->success(200,$result);
    }
    public function store(Request $request)
    {
       $created=Category::create($request->all());
        if ($created){
            return $this->message(200,'创建成功');
        }else{
            return $this->error(505,'创建失败');
        }
    }
    public function update($id,Request $request)
    {
       $update=Category::where('id',$id)->update($request->all());
        if ($update){
            return $this->message(200,'更新成功');
        }else{
            return $this->error(505,'更新失败');
        }
    }

    public function delete($id,Request $request)
    {
        $delete=Category::destroy($id);
        if ($delete){
            return $this->message(200,'删除成功');
        }else{
            return $this->error(505,'删除失败');
        }
    }
    public function tree()
    {
        $categories=Category::where([['status','=',1]])->lang($this->lang)->select([DB::raw("tid as value"),DB::raw("name as label"),"pid"])->orderBy('sort','desc')->get()->toArray();
$tree=$this->getChild($categories);
//dd($tree);
        return $this->success(200,$tree);

    }
    private function getChild($data)
    {
        $items = array();
        foreach($data as $value){
            $items[$value['value']] = $value;
        }
        //第二部 遍历数据 生成树状结构
        $tree = array();
        foreach($items as $key => $item){
            if(isset($items[$item['pid']])){
                $items[$item['pid']]['children'][] = &$items[$key];
            }else{
                $tree[] = &$items[$key];
            }
        }
        return $tree;

    }
    public function generateTree(array $items, $id, $step){
        static $tree=[];
        foreach($items as $key=>$val) {
            if($val['pid'] == $id) {
                $flg = str_repeat('―',$step*2);
                $val['name'] = $flg.$val['name'];
                $val['level']=$step;
                $tree[] = $val;
                $this->generateTree($items , $val['tid'] ,$step+1);
            }
        }
        return $tree;
    }
    public function generateTree2(array $items,$id,$filter_id,$step)
    {
        static $tree=[];
        foreach($items as $key=>$val) {
            if ($val['pid']==$filter_id){
//                $this->generateTree2($items , $val['tid'] ,$filter_id,$step+1);
                continue;
            }
            if($val['pid'] == $id) {
                $flg = str_repeat('―',$step*2);
                $val['name'] = $flg.$val['name'];
                $val['level']=$step;
                $tree[] = $val;
                $this->generateTree2($items , $val['tid'] ,$filter_id,$step+1);
            }
        }
        return $tree;
    }
}
